// -----------------------------------------------
// Author:     code34 nicolas_boiteux@yahoo.fr
// Edited by:  Neumatic
// Warcontext: Arrest a prisoner
// -----------------------------------------------

private [
	"_unit",
	"_enemy",
	"_missioncomplete",
	"_group",
	"_vehicle",
	"_vehicle2",
	"_buildings",
	"_arrayofpos",
	"_position",
	"_index"
];

_unit = _this select 0;

//A3 TODO: setVehicleInit
_unit setVehicleInit "this addAction ['<t color=''#ffcb00''>Follow me</t>', 'warcontext\actions\WC_fnc_dofollowme.sqf', [], 6, false, true];";
_unit setVariable ["wcprotected", true];
processInitCommands;

_arrayofpos = [];
_missioncomplete = false;
_unit setUnitPos "Up"; 
doStop _unit;
_group = group _unit;

_buildings = nearestObjects [position _unit, ["House"], 350];
{
	if (getDammage _x == 0) then {
		_index = 0;
		while {format ["%1", _x buildingPos _index] != "[0,0,0]"} do {
			_position = _x buildingPos _index;
			_arrayofpos = _arrayofpos + [_position];
			_index = _index + 1;
			sleep 0.05;
		};
	};
} foreach _buildings;

_position = _arrayofpos call BIS_fnc_selectRandom;
_unit setPos _position;

_vehicle = _group createUnit [(wcspecialforces call BIS_fnc_selectRandom), position _unit, [], 1, "NONE"];
_vehicle2 = _group createUnit [(wcspecialforces call BIS_fnc_selectRandom), position _unit, [], 8, "NONE"];

wcunits = wcunits + [_vehicle];
wcunits = wcunits + [_vehicle2];

// Prevent from initialisation crash, fall of roof etc.
_vehicle allowDammage false;
_vehicle2 allowDammage false;

wcgarbage = [_unit, wcskill] spawn WC_fnc_setskill;
wcgarbage = [_vehicle, wcskill] spawn WC_fnc_setskill;
wcgarbage = [_vehicle2, wcskill] spawn WC_fnc_setskill;

wcgarbage = [_group, (position (leader _group)), 100] spawn WC_fnc_patrol;

wcgarbage = [_vehicle] spawn WC_fnc_dosillything;
wcgarbage = [_vehicle2] spawn WC_fnc_dosillything;

sleep 10;

_vehicle allowDammage true;
_vehicle2 allowDammage true;

while {!_missioncomplete} do {
	_enemy = _unit findNearestEnemy position _unit;
	if (_enemy distance _unit < 8) then {
		_unit setCaptive true;
		removeAllWeapons _unit;
		_unit allowFleeing 0;
		_unit playMoveNow "AmovPercMstpSnonWnonDnon_AmovPercMstpSsurWnonDnon";
	};
	if ((getMarkerPos "jail") distance _unit < 100) then {
		wcmessageW = [localize "STR_WC_MESSAGEMISSIONCOMPLETED", localize "STR_WC_MESSAGELEAVEZONE"];
		["wcmessageW", "client"] call WC_fnc_publicvariable;
		wcmissionsuccess = true;
		_missioncomplete = true;
		wcleveltoadd = 1;
		wcfame = wcfame + wcbonusfame;
	};
	if ((!alive _unit) or (damage _unit > 0.9)) then {
		wcmessageW = [localize "STR_WC_MESSAGEMISSIONFAILED", localize "STR_WC_MESSAGELEAVEZONE"];
		["wcmessageW", "client"] call WC_fnc_publicvariable;
		wcmissionsuccess = true;
		_missioncomplete = true;
		_unit setDamage 1;
	};
	if (damage _unit > 0.1) then {
		removeAllWeapons _unit;
	};
	sleep 1;
};

if (alive _unit) then {
	_unit setPos getMarkerPos "jail";
	[_unit] joinSilent group prisoner;
	_unit allowDammage false;
	_unit setUnitPos "Up";
	doStop _unit;
	_unit playMoveNow "AmovPercMstpSnonWnonDnon_AmovPercMstpSsurWnonDnon";
	_unit disableAI "MOVE";
	_unit disableAI "ANIM";
};